package com.shareach.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.mongodb.BasicDBObjectBuilder;
import com.shareach.common.BaseQueryOrder;
import com.shareach.common.BizCondition;
import com.shareach.dao.mongo.BizTuanShopDao;
import com.shareach.domain.BizTuanShopDomain;
import com.shareach.service.BizTuanShopService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.Globals;

@Service
public class BizTuanShopServiceImpl extends BaseService<BizTuanShopDomain> implements BizTuanShopService {
	@Autowired
	private BizTuanShopDao bizTuanShopDao;

	@Override
	public synchronized Long save(BizTuanShopDomain domain) {
		domain.setCreatTime(new Date());
		Long retId = null;
		retId = bizTuanShopDao.insert(domain);
		return retId;
	}

	@Override
	public void del(BizTuanShopDomain domain) {
		if (domain.getId() == null)
			return;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(domain.getId()));
		bizTuanShopDao.delete(query, BizTuanShopDomain.class);
	}

	@Override
	public synchronized void update(BizTuanShopDomain domain, BizCondition condition) {
		// handle parse conditon
		Query query = parseConditionToQuery(condition);
		// handle update condition...
		Update update = parseDomaintoUpdate(domain);

		// update tels if necessary
		String[] tels = domain.getTels();
		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}

		if (update.getUpdateObject().toMap().size() != 0) {
			bizTuanShopDao.update(query, update);
		}
	}

	@Override
	public BizTuanShopDomain getById(Long id) {
		if (id == null)
			return null;
		Query query = new Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BizTuanShopDomain bizTuanShopDomain = bizTuanShopDao.findOne(query);
		return bizTuanShopDomain;
	}

	@Override
	public List<BizTuanShopDomain> getList(BizTuanShopDomain domain) {
		Query query = parseDomaintoQuery(domain);

		List<BizTuanShopDomain> bizTuanShopDomains = bizTuanShopDao.findList(query);
		return bizTuanShopDomains;
	}

	@Override
	public synchronized Map<String, Object> saveOrUpdate(BizTuanShopDomain domain) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			BizTuanShopDomain paradomain = new BizTuanShopDomain();
			paradomain.setSite(domain.getSite());
			paradomain.setSiteId(domain.getSiteId());
			paradomain.setName(domain.getName());
			paradomain.setAddr(domain.getAddr());
			BizTuanShopDomain retShop = bizTuanShopDao.findOne(parseDomaintoQuery(paradomain));
			// 存在则更新
			if (null != retShop) {
				update(domain, paradomain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, retShop.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_EXIST);
				return retMap;
			} else {
				save(domain);
				retMap.put(Globals.GLOBAL_SERVICE_RET_ID, domain.getId());
				retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_NOTEXIST);
				return retMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			retMap.put(Globals.GLOBAL_SERVICE_RET_CODE, Globals.GLOBAL_SERVICE_CODE_ERROR);
			return retMap;
		}

	}

	@Override
	public List<BizTuanShopDomain> getList(BizCondition condition) {
		Query query = parseConditionToQuery(condition);
		List<BizTuanShopDomain> domainList = bizTuanShopDao.findList(query);
		return domainList;
	}

	@Override
	public Page<BizTuanShopDomain> getPageResult(BizTuanShopDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BizTuanShopDomain> domainPage = bizTuanShopDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public synchronized void update(BizTuanShopDomain domain, BizTuanShopDomain contition) {
		Update update = parseDomaintoUpdate(domain);
		Query query = parseDomaintoQuery(contition);
		// if (contition.getId() != null) {
		// query.addCriteria(Criteria.where("_id").is(contition.getId()));
		// }
		// update tels if necessary
		String[] tels = domain.getTels();

		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}

		if (update.getUpdateObject().toMap().size() != 0) {
			bizTuanShopDao.update(query, update);
		}
	}

	@Override
	public synchronized void update(BizTuanShopDomain domain) {
		if (domain.getId() == null)
			return;
		Update update = parseDomaintoUpdate(domain);

		String[] tels = domain.getTels();

		if (tels != null && tels.length > 0) {
			update.addToSet("tels", BasicDBObjectBuilder.start("$each", tels).get());
		}

		if (update.getUpdateObject().toMap().size() > 0) {
			bizTuanShopDao.update(Criteria.where("_id").is(domain.getId()), update);
		}
	}

	@Override
	public List<BizTuanShopDomain> getListByIds(BizTuanShopDomain domain, List<Long> ids, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);

		if (null != ids && ids.size() > 0) {
			// Query query=new Query();
			// query.addCriteria(Criteria.where("city").is(domain.getId()));
			query.addCriteria(Criteria.where("_id").in(ids));
			query.getQueryObject();
			return bizTuanShopDao.getPageResult(query, order).getContent();
		}
		return Lists.newArrayList();
	}

}
